#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify
from flasgger import swag_from
from controller.adminInfoController import AdminInfoController
from utils import commons
from utils.response_code import RET


class AdminInfoResource(Resource):

    # get
    @swag_from('ymls/adminInfo_get.yml')
    def get(self, AutoID=None):
        if AutoID:
            kwargs = {
                'AutoID': AutoID
            }
        
            res = AdminInfoController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], error=res['error'])
                
        parser = reqparse.RequestParser()
        parser.add_argument('AdminUserID', type=str, location='args', required=False, help='AdminUserID参数类型不正确或缺失')
        parser.add_argument('AdminPassword', type=str, location='args', required=False, help='AdminPassword参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='args', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('CreateTime', type=str, location='args', required=False, help='CreateTime参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='args', required=False, help='ModifyTime参数类型不正确或缺失')
        
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Size参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        
        res = AdminInfoController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error']) 
            
    # delete
    @swag_from('ymls/adminInfo_delete.yml')
    def delete(self, AutoID=None):
        if AutoID:
            kwargs = {
                'AutoID': AutoID
            }
        
        else:
            parser = reqparse.RequestParser()
            parser.add_argument('AdminUserID', type=str, location='form', required=False, help='AdminUserID参数类型不正确或缺失')
            parser.add_argument('AdminPassword', type=str, location='form', required=False, help='AdminPassword参数类型不正确或缺失')
            parser.add_argument('IsDelete', type=int, location='form', required=False, help='IsDelete参数类型不正确或缺失')
            parser.add_argument('CreateTime', type=str, location='form', required=False, help='CreateTime参数类型不正确或缺失')
            parser.add_argument('ModifyTime', type=str, location='form', required=False, help='ModifyTime参数类型不正确或缺失')
            
            parser.add_argument('AutoID', type=str, location='form', required=False, help='AutoID参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
            
        res = AdminInfoController.delete(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    # put
    @swag_from('ymls/adminInfo_put.yml')
    def put(self, AutoID):
        if not AutoID:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')
            
        parser = reqparse.RequestParser()
        parser.add_argument('AdminUserID', type=str, location='form', required=False, help='AdminUserID参数类型不正确或缺失')
        parser.add_argument('AdminPassword', type=str, location='form', required=False, help='AdminPassword参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='form', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('CreateTime', type=str, location='form', required=False, help='CreateTime参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='form', required=False, help='ModifyTime参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['AutoID'] = AutoID
            
        res = AdminInfoController.update(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    # add
    @swag_from('ymls/adminInfo_post.yml')
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('AdminInfoList', type=str, location='form', required=False, help='AdminInfoList参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        
        if kwargs.get('AdminInfoList'):
            res = AdminInfoController.add_list(**kwargs)
              
        else:
            parser.add_argument('AdminUserID', type=str, location='form', required=True, help='AdminUserID参数类型不正确或缺失')
            parser.add_argument('AdminPassword', type=str, location='form', required=True, help='AdminPassword参数类型不正确或缺失')
            parser.add_argument('IsDelete', type=int, location='form', required=True, help='IsDelete参数类型不正确或缺失')
            parser.add_argument('CreateTime', type=str, location='form', required=True, help='CreateTime参数类型不正确或缺失')
            parser.add_argument('ModifyTime', type=str, location='form', required=True, help='ModifyTime参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
            
            res = AdminInfoController.add(**kwargs)
            
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])
